

package com.kimo.shop.admin.controller;


import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kimo.shop.bean.model.Supplier;
import com.kimo.shop.common.annotation.SysLog;
import com.kimo.shop.common.config.Constant;
import com.kimo.shop.common.exception.KimoShopBindException;
import com.kimo.shop.common.util.PageParam;
import com.kimo.shop.common.util.PrincipalUtil;
import com.kimo.shop.service.SupplierService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;

/**
 * @author kimo-taozhouchuan
 * @date 2020-08-25 15:50:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/supplier/supplier")
public class SupplierController {

    @Value("${kimo.expose.permission:}")
    private Boolean permission;

    private final SupplierService supplierService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param supplier
     * @return 分页数据
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<Supplier>> getSupplierPage(PageParam<Supplier> page, Supplier supplier) {

        IPage<Supplier> suppliers = supplierService.page(page, new LambdaQueryWrapper<Supplier>()
                .eq(Supplier::getShopId, Constant.PLATFORM_SHOP_ID)
                .like(StrUtil.isNotBlank(supplier.getSupplierName()), Supplier::getSupplierName, supplier.getSupplierName())
                .like(StrUtil.isNotBlank(supplier.getContactName()), Supplier::getContactName, supplier.getContactName())
                .like(StrUtil.isNotBlank(supplier.getContactTel()), Supplier::getContactTel, supplier.getContactTel())
                .orderByDesc(Supplier::getCreateTime)
        );

        if (BooleanUtil.isFalse(permission)) {
            for (Supplier record : suppliers.getRecords()) {
                if (Objects.nonNull(record.getContactTel()) && PrincipalUtil.isMobile(record.getContactTel())){
                    record.setContactTel(PhoneUtil.hideBetween(record.getContactTel()).toString());
                }
            }
        }
        return ResponseEntity.ok(suppliers);
    }


    /**
     * 通过id查询
     *
     * @param supplierId id
     * @return 单个数据
     */
    @GetMapping("/info/{supplierId}")
    public ResponseEntity<Supplier> getById(@PathVariable("supplierId") Long supplierId) {
        return ResponseEntity.ok(supplierService.getById(supplierId));
    }

    /**
     * 新增
     *
     * @param supplier
     * @return 是否新增成功
     */
    @SysLog("新增")
    @PostMapping
    public ResponseEntity<Boolean> save(@RequestBody @Valid Supplier supplier) {
        if (supplier.getSupplierName().length()>50) {
            // 供应商名称长度不能超过50个字符
            throw new KimoShopBindException("kimo.supplier.supplierName.length.limit");
        }
        Integer nameNumber = supplierService.getCountByName(supplier.getSupplierName());
        if (nameNumber >= 1){
            throw new KimoShopBindException("kimo.supplier.supplierName.exist");
        }
        supplier.setShopId(Constant.PLATFORM_SHOP_ID);
        supplier.setCreateTime(new Date());
        return ResponseEntity.ok(supplierService.save(supplier));
    }

    /**
     * 修改
     *
     * @param supplier
     * @return 是否修改成功
     */
    @SysLog("修改")
    @PutMapping
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid Supplier supplier) {
        if (supplier.getSupplierName().length()>50) {
            // 供应商名称长度不能超过50个字符
            throw new KimoShopBindException("kimo.supplier.supplierName.length.limit");
        }
        supplier.setUpdateTime(new Date());
        return ResponseEntity.ok(supplierService.updateById(supplier));
    }

    /**
     * 通过id删除
     *
     * @param supplierId id
     * @return 是否删除成功
     */
    @SysLog("删除")
    @DeleteMapping("/{supplierId}")
    public ResponseEntity<Boolean> removeById(@PathVariable Long supplierId) {
        return ResponseEntity.ok(supplierService.removeById(supplierId));
    }

}
